﻿using Microsoft.SharePoint;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SPMetadataSecurity
{
    public class ControlHelper
    {
        public Control GetControlForType(SPFieldType fieldType)
        {           
            Control control = null;
            switch(fieldType)
            {
                case SPFieldType.Text:
                    control = new TextBox();
                    control.ID = "MetadataValueControl" + fieldType.ToString();
                    break;
                case SPFieldType.Number:
                    control = new TextBox();
                    ((TextBox)control).TextMode = TextBoxMode.Number;
                    control.ID = "MetadataValueControl" + fieldType.ToString();
                    break;
                case SPFieldType.Boolean:
                    control = new DropDownList();
                    control.ID = "MetadataValueControl" + fieldType.ToString();
                    ((DropDownList)control).Items.Add(new ListItem("True", "true"));
                    ((DropDownList)control).Items.Add(new ListItem("False", "false"));
                    break;
            }
            return control;
        }

        public object GetControlValue(Control control, SPFieldType fieldType)
        {
            object returnValue = null;
            if(control != null)
            {
                switch(fieldType)
                {
                    case SPFieldType.Text:
                        TextBox textBox = control as TextBox;
                        returnValue = textBox.Text.Trim();
                        break;
                    case SPFieldType.Number:
                        TextBox numberTextBox = control as TextBox;
                        returnValue = numberTextBox.Text.Trim();                        
                        break;
                    case SPFieldType.Boolean:
                        DropDownList booleanList = control as DropDownList;
                        returnValue = Boolean.Parse(booleanList.SelectedValue);
                        break;
                }

            }
            return returnValue;
        }

        public void Test()
        {
            
        }
    }
}
